# -*- coding: cp1252 -*-
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.platypus import Paragraph, SimpleDocTemplate
from reportlab.lib.pagesizes import letter,A4,A5,A3
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT,TA_JUSTIFY
from reportlab.pdfgen import canvas
from conversor import Numero_a_Texto
from pylab import double,where
import os


styleSheet=getSampleStyleSheet()
styleSheet.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
styleSheet.add(ParagraphStyle(name='Left', alignment=TA_LEFT))
styleSheet.add(ParagraphStyle(name='Right', alignment=TA_RIGHT))

def traslateXY(c,P,X,Y,W,H):
	Lx, Ly = c._pagesize
	Ly+=8 ### se ajusta para cuadrar el tamaño real de la hoja de la factura
	w,h = P.wrap(W, H)	
	X0=X
	Y0=Ly-Y-h	
	return X0,Y0

def prtTxt(txt,c,W,H,X,Y,sS,fli=0):

	hh=styleSheet[sS]
	hh.firstLineIndent = fli
	P=Paragraph(txt,hh)
	X0,Y0=traslateXY(c,P,X,Y,W,H)
	P.drawOn(c,X0,Y0)
	w,h = P.wrap(W, H)
	return w,h


def coma(a):	
	w=''
	signo=[".",".",".",","]
	b=[ a[::-1].split('.')[1][3*k:3*(k+1)][::-1] for k in range(4) ][::-1]

	for k in range(4):
		if b[k]!='':
			w+=b[k]+signo[k]
	w+=a[::-1].split('.')[0]

	return w



def gen_factura(listaA, listaB):
	
	XC1=36
	XC2=118
	XC3=420
	XC4=500
		
	senores=listaA[0]
	nitocc=listaA[1]
	direccion=listaA[2]
	telefono=listaA[3]
	fechaEmision=listaA[4]
	fechaVencimiento=listaA[5]
	formaPago=listaA[6]
	anticipo=double(listaA[7])
	auxretiva=double(listaA[8])
	auxretica=double(listaA[9])
	auxretfte=double(listaA[10])

	cantidad=[]
	descripcion=[]
	vrUnitario=[]
	vrTotal=[]

	
	
	
	for k in range(len(listaB)):
		c=int(eval(listaB[k][0]))
		vu=int(eval(listaB[k][1]))
		txt=listaB[k][2]
		vt=c*vu
		cantidad.append(str(c))
		descripcion.append(txt)
		vrUnitario.append(vu)
		vrTotal.append(vt)			




	subtotal=double(vrTotal).sum()
	retiva=round(0.5*0.16*subtotal*auxretica)
	retica=round(0.01104*subtotal*auxretiva)
	iva=0.16*subtotal
	retfte=where(subtotal>663000,round(subtotal*0.035*auxretfte),0)
	total=subtotal+iva-retiva-retica-retfte

	saldo=total-anticipo

	son=Numero_a_Texto(int(total))+" PESOS M/CTE"



	name="ultima_factura.pdf"


	c=canvas.Canvas(name, pagesize=letter)
	print c.getAvailableFonts()
	#~ c.setFont("Helvetica",26)
	#~ c.setFont("Times-Roman",26)
	#~ c.setFont("Courier",25)



	X0=94
	Y0=164
	dY=21
	prtTxt(txt=senores,c=c,W=300,H=100,X=X0,Y=Y0,sS='Normal')
	prtTxt(txt=nitocc,c=c,W=150,H=100,X=X0,Y=Y0+dY,sS='Normal')
	prtTxt(txt=direccion,c=c,W=150,H=100,X=X0,Y=Y0+2*dY,sS='Normal')
	prtTxt(txt=telefono,c=c,W=150,H=100,X=X0,Y=Y0+3*dY,sS='Normal')

	prtTxt(txt=fechaEmision,c=c,W=100,H=100,X=448,Y=186,sS='Normal')
	prtTxt(txt=fechaVencimiento,c=c,W=100,H=100,X=448,Y=208,sS='Normal')
	prtTxt(txt=formaPago,c=c,W=100,H=100,X=448,Y=230,sS='Normal')

	Y0=286
	X0=[XC1,XC2,XC3,XC4]
	for k in range(len(cantidad)):
		prtTxt(txt=cantidad[k],c=c,W=50,H=100,X=X0[0],Y=Y0,sS='Right')
		w,h=prtTxt(txt=descripcion[k],c=c,W=273,H=100,X=X0[1],Y=Y0,sS='Justify')
		prtTxt(txt="$ "+coma("%.2f"%vrUnitario[k]),c=c,W=55,H=100,X=X0[2],Y=Y0,sS='Right')
		prtTxt(txt="$ "+coma("%.2f"%vrTotal[k]),c=c,W=80,H=100,X=X0[3],Y=Y0,sS='Right')
		
		Y0+=(h+10)	


	Y0=563
	dY=23
	W0=75

	prtTxt(txt="$ "+coma("%.2f"%retiva),c=c,W=W0,H=100,X=XC4,Y=Y0,sS='Right')
	prtTxt(txt="$ "+coma("%.2f"%retica),c=c,W=W0,H=100,X=XC4,Y=Y0+dY,sS='Right')
	prtTxt(txt="$ "+coma("%.2f"%iva),c=c,W=W0,H=100,X=XC4,Y=Y0+2*dY,sS='Right')
	prtTxt(txt="$ "+coma("%.2f"%retfte),c=c,W=W0,H=100,X=XC4,Y=Y0+3*dY,sS='Right')
	prtTxt(txt="$ "+coma("%.2f"%total),c=c,W=W0,H=100,X=XC4,Y=Y0+4*dY,sS='Right')
	prtTxt(txt="$ "+coma("%.2f"%anticipo),c=c,W=W0,H=100,X=XC4,Y=Y0+5*dY,sS='Right')
	prtTxt(txt="$ "+coma("%.2f"%saldo),c=c,W=W0,H=100,X=XC4,Y=Y0+6*dY,sS='Right')

	prtTxt(txt=son,c=c,W=345,H=20,X=46,Y=565,sS='Left',fli=25)

	c.save()



	os.system("evince " + name + "&")	
	


